<template>
	<view class="container" style="height: 100vh;">
		<view style="margin-top: 30%;text-align: center;font-size: 30px;font-weight: bolder;">
			<text >请绑定你的教练</text>
		</view>
		<view style="text-align: center;color: #878787;">
		<text >注意：教练邀请码需要你的教练告知</text>
		</view>
		<view style="margin-top: 25%;height: 3vh;display: flex; justify-content: center;">
			<input type="text" class="custom-input" v-model="invitationCode" placeholder=" 请输入教练邀请码" style=" font-size: 3vh;" @input="onInvitationCode">
		</view>
		<view style="margin-top: 48%;display: flex;justify-content: space-around;">
		      <button class="custom-button" @click="skip" style=" border: 1px solid #bcbcbc;color: rgb(152, 152, 152);">跳过</button>
		      <button class="custom-button" @click="haveCoach" style="background-color:rgb(148, 171, 253);color: aliceblue;">确定 &#9658;</button>
		    </view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				invitationCode:''
			}
		},
		methods: {
			// 邀请码字符判断
			onInvitationCode: function(){
				 if(this.invitationCode.length === 14){
				    const reg = /^[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-z0-9]{4}$/;
				    if(reg.test(this.invitationCode)){

				    }else{
				      uni.showToast({
				        title: '邀请码格式错误',
				        icon: 'none',
				        duration: 2000,
						backgroundColor: '#ff0000' // 设置为红色
				      });
				    }
				  }
			},
			// 跳过按钮操作
			skip () {
				uni.navigateTo({
					url: '/pages/student/chooseLabel/welcome',
					animationType: 'slide-out-top',
					animationDuration: 200
				});
			},
			// 添加邀请码操作
			haveCoach () {
				 this.$globalBus.stopTimer()
				uni.request({
					url: "http://127.0.0.1:17000/memberUser/selectByInvitationCode",
					method: "GET",
					data: {
						id:uni.getStorageSync('userInfo').id,
						invitationCode: this.invitationCode,
						venueUniqueIdentifier: uni.getStorageSync('userInfo').venueUniqueIdentifier
					},
					success: (res) => {
						console.log(res.data.data.data)
						if(res.data.data.data === null){
							uni.showToast({
							  title: '邀请码输入错误或不存在',
							  icon: 'none',
							  duration: 2000,
							  backgroundColor: '#ff0000' // 设置为红色
							});
						}else if(res.data.data.data === "you"){
							uni.showToast({
							  title: '已提交过相关申请',
							  icon: 'none',
							  duration: 1500,
							  backgroundColor: '#ff0000' // 设置为红色
							});
							this.$globalBus.startTimer()
							setTimeout(function() {
							    uni.navigateTo({
							        url: '/pages/student/chooseLabel/welcome',
							        animationType: 'slide-out-top',
							        animationDuration: 200
							    });
							}, 1500);
						}else{
							 uni.showToast({
							        title: '已提交申请，请等待教练同意',
							        icon: 'none',
							        duration: 1500,
							        backgroundColor: '#ff0000' // 设置为红色
							    });
							this.$globalBus.startTimer()
							    setTimeout(function() {
							        uni.navigateTo({
							            url: '/pages/student/chooseLabel/welcome',
							            animationType: 'slide-out-top',
							            animationDuration: 200
							        });
							    }, 1500);
						}
					}
					})
			}
		}
	}
</script>

<style>
.custom-button {
  padding: 10px 20px;
  border-radius: 25px;
  width: 35vw;
  font-size: 15px;
  display: flex;
    align-items: center;
    justify-content: center;
}
.custom-input {
	border: 1px solid blue;
	border-radius: 30px;
	width: 300px; /* 自定义长度 */
	height: 6vh;
}

</style>
